package ch08
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * copyFrom():将源 Graphics 对象中的所有绘画命令复制到执行调用的 Graphics 对象中。 
	 * graphics.copyFrom应用
	 */
	[SWF(width=500,height=500,backgroundColor=0x000000)]
	public class CopyFromTest extends Sprite
	{
		//舞台旋转的速度
		private const ROTATION_SPEED:Number = 1;
		//复制图形的个数
		private const SHAPE_NUM:int = 10;

		//存储舞台的长宽
		private var sWidth:Number = stage.stageWidth;
		private var sHeight:Number = stage.stageHeight;

		//存放shape的容器
		private var _shapeHolder:Sprite;
		private var _shapeArray:Vector.<Shape>;
		private var _firstShape:Shape;
		private var _color:uint;

		public function CopyFromTest()
		{
			_color = Math.random() * 0xffffff;

			_shapeHolder = new Sprite();
			_shapeHolder.x = sWidth / 2;
			_shapeHolder.y = sHeight / 2;
			_shapeHolder.filters = [new GlowFilter(_color, 1, 20, 20)];
			addChild(_shapeHolder);
			//将shape图形存储在_shapeArray里方便遍历
			_shapeArray = new Vector.<Shape>();

			//添加并放置shape图形
			for (var i:int = 0; i < SHAPE_NUM; i++)
			{
				addShape();
			}
			positionShape();
			//配置事件侦听
			configEvent();
		}

		private function configEvent():void
		{
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}

		//                事件侦听器======================================
		private function onStageMouseDown(me:MouseEvent):void
		{
			_firstShape.graphics.lineStyle(1, _color);
			//鼠标按下后,线条的起始点移至_firstShape相对鼠标坐标处
			_firstShape.graphics.moveTo(_firstShape.mouseX, _firstShape.mouseY);

			stage.addEventListener(Event.ENTER_FRAME, loop);
		}

		private function onStageMouseUp(me:MouseEvent):void
		{
			stage.removeEventListener(Event.ENTER_FRAME, loop);
		}

		private function loop(e:Event):void
		{
			//旋转所有shape的容器_shapeHolder
			_shapeHolder.rotation += ROTATION_SPEED;
			draw();
		}

		//                画图方法========================================
		private function draw():void
		{
			//除了第一个shape我们进行绘画外,其他的我们直接copy第一个shape的
			_firstShape.graphics.lineTo(_firstShape.mouseX, _firstShape.mouseY)
			for (var i:int = 1; i < SHAPE_NUM; i++)
			{
				_shapeArray[i].graphics.copyFrom(_firstShape.graphics);
			}

		}

		//                添加shape图形====================================
		private function addShape():void
		{
			var sp:Shape = new Shape();
			if (_shapeArray.length > 0)
			{
				sp.graphics.copyFrom(_firstShape.graphics);
			}
			else
			{
				_firstShape = sp;
			}
			_shapeArray.push(sp);
			_shapeHolder.addChild(sp);
		}

		//                布置shape图形======================================
		private function positionShape():void
		{
			for (var i:int = 1; i < SHAPE_NUM; i++)
			{
				_shapeArray[i].rotation = 360 * i / SHAPE_NUM;
			}
		}

	}
}